package fr.ordre66.vlcs.client;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GraphicsEnvironment;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.lang.reflect.InvocationTargetException;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class IHM extends JFrame{
	
	private static final long serialVersionUID = -3355034413949066626L;

	private BufferedImage image;
	private ImagePane imagePane;
	private SendImageThread sendImg;
	private SendVideoThread sendVideo;
	private Config configFrame = new Config();
	public static int WIDTH = 375;
	public static int HEIGHT = 250;
	JMenuItem mntmSurveillance = new JMenuItem("Start surveillance");
	JMenuItem mntmStreaming = new JMenuItem("Start streaming");
	private Player player;
	
	public IHM(final Player _player) throws InterruptedException, InvocationTargetException {

		this.player = _player;
		
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		
		sendImg = new SendImageThread(this);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnNewMenu = new JMenu("Fichier");
		menuBar.add(mnNewMenu);
		
		
		mntmSurveillance.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {

				if(sendImg.isAlive()){
					sendImg.interrupt();
					imagePane.setMsg("");
					mntmSurveillance.setText("Start surveillance");
				}
				else{
					sendImg.start();
					imagePane.setMsg("Connected");
					mntmSurveillance.setText("Stop surveillance");
				}
			}
		});
		
		JMenuItem mntmConfig = new JMenuItem("Config");
		mntmConfig.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				configFrame.setVisible(true);
			}
		});
		
		mnNewMenu.add(mntmConfig);
		mnNewMenu.add(mntmSurveillance);
				
		mntmStreaming.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				if("Start streaming".equals(mntmStreaming.getText())){
					player.startStreaming();
					imagePane.setMsg("Streaming");
					mntmStreaming.setText("Stop streaming");
				}
				else{
					player.stopStreaming();
					imagePane.setMsg("");
					mntmStreaming.setText("Start streaming");
				}
			}
		});
		mnNewMenu.add(mntmStreaming);
		
		JMenuItem mntmNewMenuItem = new JMenuItem("Exit");
		mntmNewMenuItem.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				dispose();
				System.exit(0);
			}
		});
		mnNewMenu.add(mntmNewMenuItem);
		
		
		image = GraphicsEnvironment.getLocalGraphicsEnvironment()
		.getDefaultScreenDevice().getDefaultConfiguration()
		.createCompatibleImage(WIDTH, HEIGHT);
		image.setAccelerationPriority(1.0f);
		getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		imagePane = new ImagePane(image);
		imagePane.setSize(WIDTH, HEIGHT);
		imagePane.setMinimumSize(new Dimension(WIDTH, HEIGHT));
		imagePane.setPreferredSize(new Dimension(WIDTH, HEIGHT));
		getContentPane().add(imagePane);
		pack();
		setResizable(false);
		setVisible(true);
	}
	
	public BufferedImage getImage() {
		return image;
	}

	public void setImage(BufferedImage image) {
		this.image = image;
	}

	public ImagePane getImagePane() {
		return imagePane;
	}

	public void setImagePane(ImagePane imagePane) {
		this.imagePane = imagePane;
	}

	public Config getConfigFrame() {
		return configFrame;
	}

	public Player getPlayer() {
		return player;
	}
	
	
}
